package com.ubst.smartad.impl;

import java.sql.*;
import java.util.*;
import java.io.*;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Vector;

import com.ubst.smartad.dao.TSAAdminSysGroupDO;
import com.ubst.smartad.common.TimeUtil;
import com.ubst.smartad.logger.Debug;

public class TSAAdminSysGroupMngImpl {
	
	Connection con = null;
	private String serverName;
	private Debug debug;

	/** 
     * 생성자
     *   
     *   
    */ 
	public TSAAdminSysGroupMngImpl(Connection con, String serverName, String log_level) {
		this.con = con;
		this.serverName = serverName;
		this.debug = new Debug("TSAAdminSysGroupMngImpl", log_level);

	}
	
    /** 
	 * @author KJH
     * 그룹관리자 사용 그룹등록
     *   
     */  
    public void insertAdminSysGroup(TSAAdminSysGroupDO sysGroupDO) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

			long l_cur_date = TimeUtil.getCurrentTime();

            queryBuf = new StringBuffer();
			queryBuf.append("INSERT INTO TSA_ADMIN_SYSGROUP " )
				.append(" ( ")
				.append("  USERID ,GROUPID ,CREATEDATE ")
				.append(" ) ")
				.append(" VALUES")
				.append(" ( ") 
				.append("  ? ,? ,? ")
				.append(" ) ");

			debug.debug("insertAdminSysGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, sysGroupDO.userid);
			pstmt.setString(idx++, sysGroupDO.groupid);
			pstmt.setString(idx++, TimeUtil.getDateString(l_cur_date, "yyyyMMddHHmmss"));

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

	/**
	 * @author : KJH 
	 * 그룹관리자의 그룹정보 리스트 출력
	 *
	 */
	public Vector selectAdminSysGroupList(String userid) throws SQLException {

		PreparedStatement pstmt=null;
		ResultSet rs = null;

		Vector sysGroupDOList = new Vector();
		TSAAdminSysGroupDO sysGroupDO = null;

		try {
			
			StringBuffer queryBuf = new StringBuffer();
			queryBuf.append("SELECT ")
				.append("  GROUPID ,CREATEDATE ")
				.append(" FROM TSA_ADMIN_SYSGROUP ")
				.append(" WHERE USERID=? ");

			debug.debug("selectAdminSysGroupList ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, userid);

			rs = pstmt.executeQuery();

			while ( rs.next() ){
				sysGroupDO = new TSAAdminSysGroupDO();
				sysGroupDO.userid = userid;
				sysGroupDO.groupid = rs.getString("GROUPID");
				sysGroupDO.createdate = rs.getString("CREATEDATE");

				sysGroupDOList.add(sysGroupDO);
				sysGroupDO = null;
			}

			rs.close();
			pstmt.close();

		}catch (SQLException e){
			throw e;
		}finally{
			if ( rs != null) try{rs.close();}catch(SQLException ee){}
			if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
		}

		return sysGroupDOList;
	}

    /** 
	 * @author KJH
     * 그룹사용자의 그룹삭제
     *   
     */  
    public void deleteAdminSysGroup(String userid, String groupid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_ADMIN_SYSGROUP " )
				.append(" WHERE USERID=? AND GROUPID=? ");

			debug.debug("deleteAdminSysGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, userid);
			pstmt.setString(idx++, groupid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

    /** 
	 * @author KJH
     * 그룹사용자의 전체그룹삭제
     *   
     */  
    public void deleteAllAdminSysGroup(String userid) throws SQLException{
        PreparedStatement pstmt=null;

        StringBuffer queryBuf = null;
        try {

            queryBuf = new StringBuffer();
			queryBuf.append("DELETE FROM TSA_ADMIN_SYSGROUP " )
				.append(" WHERE USERID=? ");

			debug.debug("deleteAllAdminSysGroup ==> "+queryBuf.toString());

			pstmt = con.prepareStatement( queryBuf.toString() );
			int idx = 1;
			pstmt.setString(idx++, userid);

            pstmt.execute();
            pstmt.close();
			queryBuf = null;

        }catch (SQLException e){
			throw e;
        }finally{
            if ( pstmt != null) try{pstmt.close();}catch(SQLException ee){}
        }

    }

}
